package com.scanner.hardware;

import com.scanner.barcodeservice.SerialPortService;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class SettingsActivity extends Activity {
	private static final String TAG = "SettingsActivity"; 
	
	public SerConnection  mSerConnection;
	SerialPortService.MyBinder  mBinder = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		SettingFragment settingFragment = new SettingFragment();

		transaction.add(R.id.container,settingFragment);
		transaction.commitAllowingStateLoss();
		
		Log.d(TAG, "onCreate");
		
	    Intent mIntent = new Intent();
	    ComponentName componentName = new ComponentName(getPackageName(),"com.android.barcodeservice.SerialPortService");
	    mIntent.setComponent(componentName);
	    mSerConnection = new SerConnection();
		bindService(mIntent, mSerConnection, Context.BIND_AUTO_CREATE);
		
	}

	  @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MyApplication.onSettings = true;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MyApplication.onSettings = false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		unbindService(mSerConnection);
	}	
  
  public class SerConnection implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			  mBinder = (SerialPortService.MyBinder) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			   mBinder = null;
		}
	}

}
